/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package peticionweb.control;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import peticionweb.logica.entidad.Cuper1m;
import peticionweb.logica.entidad.Cucar1m;
import peticionweb.logica.entidad.Cupar1a;
import peticionweb.logica.entidad.Cupar1aPK;
import peticionweb.logica.entidad.Cuyea1m;
import peticionweb.control.exceptions.NonexistentEntityException;
import peticionweb.control.exceptions.PreexistingEntityException;

/**
 *
 * @author icardenas
 */
public class Cupar1aJpaController implements Serializable {

    public Cupar1aJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Cupar1a cupar1a) throws PreexistingEntityException, Exception {
        if (cupar1a.getCupar1aPK() == null) {
            cupar1a.setCupar1aPK(new Cupar1aPK());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Cuper1m cuper1m = cupar1a.getCuper1m();
            if (cuper1m != null) {
                cuper1m = em.getReference(cuper1m.getClass(), cuper1m.getCidpe());
                cupar1a.setCuper1m(cuper1m);
            }
            Cucar1m cucar1m = cupar1a.getCucar1m();
            if (cucar1m != null) {
                cucar1m = em.getReference(cucar1m.getClass(), cucar1m.getCodca());
                cupar1a.setCucar1m(cucar1m);
            }
            Cuyea1m cuyea1m = cupar1a.getCuyea1m();
            if (cuyea1m != null) {
                cuyea1m = em.getReference(cuyea1m.getClass(), cuyea1m.getIdeye());
                cupar1a.setCuyea1m(cuyea1m);
            }
            em.persist(cupar1a);
            if (cuper1m != null) {
                cuper1m.getCupar1aList().add(cupar1a);
                cuper1m = em.merge(cuper1m);
            }
            if (cucar1m != null) {
                cucar1m.getCupar1aList().add(cupar1a);
                cucar1m = em.merge(cucar1m);
            }
            if (cuyea1m != null) {
                cuyea1m.getCupar1aList().add(cupar1a);
                cuyea1m = em.merge(cuyea1m);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCupar1a(cupar1a.getCupar1aPK()) != null) {
                throw new PreexistingEntityException("Cupar1a " + cupar1a + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Cupar1a cupar1a) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Cupar1a persistentCupar1a = em.find(Cupar1a.class, cupar1a.getCupar1aPK());
            Cuper1m cuper1mOld = persistentCupar1a.getCuper1m();
            Cuper1m cuper1mNew = cupar1a.getCuper1m();
            Cucar1m cucar1mOld = persistentCupar1a.getCucar1m();
            Cucar1m cucar1mNew = cupar1a.getCucar1m();
            Cuyea1m cuyea1mOld = persistentCupar1a.getCuyea1m();
            Cuyea1m cuyea1mNew = cupar1a.getCuyea1m();
            if (cuper1mNew != null) {
                cuper1mNew = em.getReference(cuper1mNew.getClass(), cuper1mNew.getCidpe());
                cupar1a.setCuper1m(cuper1mNew);
            }
            if (cucar1mNew != null) {
                cucar1mNew = em.getReference(cucar1mNew.getClass(), cucar1mNew.getCodca());
                cupar1a.setCucar1m(cucar1mNew);
            }
            if (cuyea1mNew != null) {
                cuyea1mNew = em.getReference(cuyea1mNew.getClass(), cuyea1mNew.getIdeye());
                cupar1a.setCuyea1m(cuyea1mNew);
            }
            cupar1a = em.merge(cupar1a);
            if (cuper1mOld != null && !cuper1mOld.equals(cuper1mNew)) {
                cuper1mOld.getCupar1aList().remove(cupar1a);
                cuper1mOld = em.merge(cuper1mOld);
            }
            if (cuper1mNew != null && !cuper1mNew.equals(cuper1mOld)) {
                cuper1mNew.getCupar1aList().add(cupar1a);
                cuper1mNew = em.merge(cuper1mNew);
            }
            if (cucar1mOld != null && !cucar1mOld.equals(cucar1mNew)) {
                cucar1mOld.getCupar1aList().remove(cupar1a);
                cucar1mOld = em.merge(cucar1mOld);
            }
            if (cucar1mNew != null && !cucar1mNew.equals(cucar1mOld)) {
                cucar1mNew.getCupar1aList().add(cupar1a);
                cucar1mNew = em.merge(cucar1mNew);
            }
            if (cuyea1mOld != null && !cuyea1mOld.equals(cuyea1mNew)) {
                cuyea1mOld.getCupar1aList().remove(cupar1a);
                cuyea1mOld = em.merge(cuyea1mOld);
            }
            if (cuyea1mNew != null && !cuyea1mNew.equals(cuyea1mOld)) {
                cuyea1mNew.getCupar1aList().add(cupar1a);
                cuyea1mNew = em.merge(cuyea1mNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Cupar1aPK id = cupar1a.getCupar1aPK();
                if (findCupar1a(id) == null) {
                    throw new NonexistentEntityException("The cupar1a with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Cupar1aPK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Cupar1a cupar1a;
            try {
                cupar1a = em.getReference(Cupar1a.class, id);
                cupar1a.getCupar1aPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cupar1a with id " + id + " no longer exists.", enfe);
            }
            Cuper1m cuper1m = cupar1a.getCuper1m();
            if (cuper1m != null) {
                cuper1m.getCupar1aList().remove(cupar1a);
                cuper1m = em.merge(cuper1m);
            }
            Cucar1m cucar1m = cupar1a.getCucar1m();
            if (cucar1m != null) {
                cucar1m.getCupar1aList().remove(cupar1a);
                cucar1m = em.merge(cucar1m);
            }
            Cuyea1m cuyea1m = cupar1a.getCuyea1m();
            if (cuyea1m != null) {
                cuyea1m.getCupar1aList().remove(cupar1a);
                cuyea1m = em.merge(cuyea1m);
            }
            em.remove(cupar1a);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Cupar1a> findCupar1aEntities() {
        return findCupar1aEntities(true, -1, -1);
    }

    public List<Cupar1a> findCupar1aEntities(int maxResults, int firstResult) {
        return findCupar1aEntities(false, maxResults, firstResult);
    }

    private List<Cupar1a> findCupar1aEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Cupar1a.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Cupar1a findCupar1a(Cupar1aPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Cupar1a.class, id);
        } finally {
            em.close();
        }
    }

    public int getCupar1aCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Cupar1a> rt = cq.from(Cupar1a.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
